import {reactive,onMounted,toRefs} from '@vue/composition-api'
import {getjoinedList, useabelActivity} from "@/api/activity";
import {Toast} from 'vant'
function useList (router){
    const useDataUp=reactive({
        tabActive: 0, //切换的索引
        loading:{  //加载数据
            current: 0,
            size: 10,
            type: 0,
            total:0,//总条数
            list:[],
            isLoading: false,
            isFinished: false,
            isRefreshing: false,
        },

    })
    onMounted(()=>{
        if(useDataUp.tabActive===0){
            onListLoad(0)
        }
    })
    //重置数据
    const  onRead=()=>{
        useDataUp.loading.current=0
        useDataUp.loading.list=[]
        useDataUp.loading.total=0
        useDataUp.loading.isLoading=false
        useDataUp.loading.isRefreshing=false
        useDataUp.loading.isFinished=false
        if(useDataUp.tabActive===0){

        }if(useDataUp.tabActive===1){

        }
    }
    //切换
    const onToggle=(index)=>{
        if(useDataUp.tabActive===index){
            return
        }
        useDataUp.tabActive=index
        useDataUp.loading.type=useDataUp.tabActive
        onRead()
        if(index===0){
            onListLoad(0)
        }else{
            
            onListLoad(1)
        }
    }

    const pageNoLable=()=>{
        useDataUp.loading.current++
    }
    //列表数据
    const onListLoad=(index)=>{
        pageNoLable()
        if(index===0){
            getMoveTypeList(getjoinedList)
        }else{
            getMoveTypeList(useabelActivity)
        }

    }
    //我的活动相关列表
    const getMoveTypeList=(cb)=>{
        const {loading} = useDataUp
        const params = {
            current: loading.current,
            size: loading.size,
            type: loading.type,
        }
        loading.isLoading = true
        cb(params).then(res=>{
            console.log(res.data.records,"2");
            const {
                code,
                data,
                msg
            } = res
            if(code===0){
                console.log(res);
                if(res.data === null){
                    loading.isLoading = false
                    loading.isFinished = true
                }
                if(loading.current<=data.pages){
                    if(!data || !Object.keys(data).length){
                        loading.isFinished = true
                        return
                    }
                    const { records: list, total } = data
                    useDataUp.loading.total=total
                    if(!list || !list.length ||  list==='' || list===null){
                        loading.isFinished = true
                        return
                    }
                    loading.list = [...loading.list,...list]
                    loading.isLoading = false
                    if(loading.list.length>= useDataUp.loading.total){
                        loading.isFinished = true
                    }
                }else{
                    loading.isFinished = true
                }


            } else{
                Toast(msg)
            }
        }).catch(e=>{
            console.log ( e )
        })
    }

    const getDetail=(item,name='ActivityDetail')=>{
        router.push({
            name,
            query: { id: item.id, type: useDataUp.tabActive,brandId:item.brandId },
        })
    }
    return {
        ...toRefs(useDataUp),
        getDetail,
        onToggle,
        onRead,
        onListLoad
    }

}
export default  useList
